
<template>
  <div class="header_information page-container">
    <!-- 头部 -->
    <el-row class="header_name">
      <el-col
        :span="24"
        class="header_text"
      >
        <h1>分组管理</h1>
      </el-col>
      <el-col :span="24">
        <div class="header_line"></div>
      </el-col>
    </el-row>
    <!-- 按钮 -->
    <el-row class="el_row_btn">
      <el-col
        :span="24"
        class="content"
      >
        <div class="content_top_btn">
          <div>
            <el-button
              size="large"
              :icon="Plus"
              type="primary"
              @click="handleOpenEventedModal"
            >新增分组</el-button>

          </div>
          <!-- <div>
            <el-input
              class="top_search_inp"
              v-model="query_data.studentName"
              placeholder="请输入分组姓名"
            />
            <el-button
              size="large"
              :icon="Search"
              circle
              :loading="loading"
              @click="changeInp"
            />
          </div> -->
        </div>
      </el-col>
    </el-row>
    <!-- 内容 -->
    <el-row>
      <el-col :span="24">
        <el-table
          v-loading="loading"
          empty-text="暂无数据..."
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            label="ID"
            prop="groupId"
            width="150"
          />
          <el-table-column
            prop="groupDescribe"
            label="分组名称"
          />

          <el-table-column
            label="操作"
            width="100"
          >
            <template #default="scope">
              <el-dropdown
                trigger="click"
                @command="(value)=>handleOperate(value,scope.row)"
              >
                <span class="el_dropdown_link">
                  操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item
                      :icon="Edit"
                      command="edit-info"
                    > 编辑 </el-dropdown-item>
                    <el-dropdown-item
                      :icon="Delete"
                      command="delect-item"
                    > 删除 </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>

          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 编辑信息 -->
    <el-dialog
      v-model="info_modal"
      title="编辑分组信息"
      class="info-modal"
      width="30%"
    >
      <el-form
        :model="addOrcompile"
        ref="reg_data_ref"
        :rules="reg_data_rules"
      >
        <el-form-item
          label="分组名称"
          :label-width="formLabelWidth"
          prop="groupDescribe"
        >
          <el-input
            v-model="addOrcompile.groupDescribe"
            autocomplete="off"
            clearable
            placeholder="请输入分组名称"
            maxlength="15"
            show-word-limit
          />
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="info_modal = false">取消</el-button>
          <el-button
            type="primary"
            @click="handleSaveGroup"
            color="#1964d6"
          >
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 新增分组 -->
    <div class="pagination">
      <el-dialog
        v-model="dialogGroups"
        title="新增分组"
        class="info-modal"
        width="30%"
      >
        <el-form
          :model="addOrcompile"
          ref="reg_data_ref"
          :rules="reg_data_rules"
        >
          <el-form-item
            label="分组名称"
            :label-width="formLabelWidth"
            prop="groupDescribe"
          >
            <el-input
              clearable
              v-model="addOrcompile.groupDescribe"
              autocomplete="off"
              placeholder="请输入分组名称"
            />
          </el-form-item>

        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogGroups = false">取消</el-button>
            <el-button
              type="primary"
              color="#1964d6"
              @click="handleAddgroup"
            >
              保存
            </el-button>
          </span>
        </template>
      </el-dialog>
      <el-pagination
        background
        layout="prev, pager, next, total"
        :total="total"
        @current-change="handlePage"
      />
    </div>
  </div>
</template>

<script  setup>
import {
  Edit, Delete, Search, Plus
} from "@element-plus/icons-vue";
import { onMounted, ref, watch } from "vue";
import { usePage } from "@/hooks/usePage"

import { ElMessage, ElMessageBox } from 'element-plus';
import { getAllStudent, getGroupListPage, deleteGroup, saveGroup, updateGroup } from "@/api/manage/organcommitmenu/index"
import { withdrawalRegistration, auditCompetitionStudent } from '@/api/manage/agentmenu/index.js'

// 禁止空格
const inputValueTrim = ''
const formLabelWidth = ref(100)
// 编辑
const addOrcompile = ref({
  groupId: '',
  groupDescribe: ''
})

const handleSaveGroup = async () => {
  reg_data_ref.value.validate(async (v) => {
    if (!v) {
      return;
    }
    let res = await updateGroup(addOrcompile.value)

    if (res.success) {
      ElMessage.success(res.message);
      info_modal.value = false
      handleLoad(true);
    } else {
      ElMessage.error(res.message)
    }
    addOrcompile.value = {}
  });
}


const reg_data_ref = ref();
const reg_data_rules = ref({
  groupDescribe: [
    {
      type: 'string',
      required: true,
      message: '请输入分组名称',
      trigger: 'blur',
    },
    //不能全部输入空格
    {
      required: true,
      transform: (value) => value && value.trim(),
      message: '分组名称不能全部为空',
      trigger: 'blur'
    }
  ],
});


// 保存新增
const handleAddgroup = async () => {
  reg_data_ref.value.validate(async (v) => {
    if (!v) {
      return;
    }
    let res = await saveGroup({ groupDescribe: addOrcompile.value.groupDescribe })

    if (res.success) {
      ElMessage.success(res.message);
      dialogGroups.value = false
      handleLoad(true);
    } else {
      ElMessage.error(res.message)
    }
  });
}


/**
 * 信息弹窗数据
 */
const info_modal = ref(false)

/**
 * 操作
 */
const handleOperate = (command, row) => {

  if (command == 'edit-info') {
    // 编辑
    info_modal.value = true;
    const find = tableData.value.find((item) => row.groupDescribe == item.groupDescribe)

    if (!find) {
      return
    }
    addOrcompile.value.groupDescribe = find.groupDescribe
    addOrcompile.value.groupId = find.groupId
  } else if (command == 'delect-item') {
    // 删除
    ElMessageBox.confirm('是否删除该分组', "提示", {
      "cancel-button-text": "取消",
      "confirm-button-text": "确定",
    }).then(async () => {
      let res = await deleteGroup(
        row.groupId
      )
      if (!res.success) {
        ElMessage.error(res.message);
      } else {
        handleLoad(true);
        ElMessage.success(res.message);
      }
    })
  }
}


// 新增分组
const dialogGroups = ref(false)
const handleOpenEventedModal = () => {
  dialogGroups.value = true
  addOrcompile.value = {}
}


// 修改参赛审核状态
// studentId=5&status=3
const handlechange = async (row) => {
  let res = await auditCompetitionStudent({ studentId: row.studentId, status: row.entryState, eventId: row.eventId })

  if (res.success) {
    ElMessage({
      title: '成功',
      message: `${res.message}`,
      type: 'success',
    })
  } else {
    ElMessage.error(res.message)
  }
}
const input = ref('')
const {
  total: total,
  data: tableData,
  query_data,
  loading,
  handleLoad,
  handlePage
} = usePage({
  api_fn: getGroupListPage,
})

onMounted(() => {
  // handleGetAllStudent()
  handleLoad(true);
})
</script>

<style lang="scss" scoped>
.header_information {
  padding: 20px;
  .header_name {
    margin-left: 10px;
    margin-right: 10px;
    .header_text {
      margin-left: -10px;
      margin-right: -10px;
    }
    h1 {
      font-size: 28px;
    }
    // 线
    .header_line {
      display: block;
      height: 1px;
      width: 100%;
      margin: 24px 0 20px 0;
      background-color: #8473f717;
    }
  }
  //   内容
  :deep(.table_btn_success) {
    padding: 0 5px !important;
    border: none;
  }
  :deep(.el-table) .cell {
    padding: 0 10px;
  }
  .el_row_btn {
    margin-bottom: 20px;
    .content {
      .content_top_btn {
        display: flex;
        justify-content: space-between;
        .top_search_inp {
          width: 300px;
          height: 40px;
          margin-right: 5px;
        }
        .top_search_btn {
          width: 110px;
          margin-right: 5px;
        }
      }
    }
  }
  .el-radio {
    margin: 0 0 0 5px;
  }
  .el_dropdown_link {
    font-size: 14px;
  }
  :deep(.el-table)__cell {
    padding: 12px 0 !important;
  }
  // 分页
  .pagination {
    margin-top: 30px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50px;
  }
}
</style>
